package imp

/**
  * 隐式类
  */
object ImplicitClass extends App {

  // 只能作为构造方法定义
  implicit class Dog(val name: String) {
    def bark(): Unit = println(s"$name say: Wang !")
  }

  //只能携带一个非隐式参数
  implicit class Animal(val names: Seq[String])(implicit index: Int) {
    def bark(): Unit = println(s"$names[index] say: Aou !")
  }

  val Animal = 1

  //string没有bark方法，隐式转换成Dog，就可以bark了
  "Teddy".bark()

  // Seq集合和下表index，隐式转换成Animal
  Seq("Teddy", "Lvmao", "Zangao")(0).bark()
  Seq("Teddy", "Lvmao", "Zangao")(1).bark()
  Seq("Teddy", "Lvmao", "Zangao")(2).bark()
}
